<template>
  <div class="container">
    <gantt v-loading="loading" class="left-container" ref="gt" :tasks="tasks"></gantt>
  </div>
</template>
 
<script>
import Gantt from "@/components/Gantt";
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
} from "@/api/myTask/task";
import { listBaseInfoDict } from "@/api/project/baseInfo";
export default {
  name: "app",
  components: { Gantt },
  dicts: ["sys_yes_no", "project_task_status"],
  data() {
    return {
      tasks: {
        data: [],
        links: [],
      },
      // 查询参数
      queryParams: {
        projectId: null,
        tasksName: null,
        planBeginTime: null,
        planEndTime: null,
        taskOwner: null,
        taskStatus: null,
        isMilestone: null,
      },
      loading:false
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    /** 查询项目任务信息列表 */
    getList() {
      this.loading = true;
      this.tasks = {
        data: [],
        links: [],
      };
      listTask(this.queryParams).then((response) => {
        this.taskList = this.handleTree(response.data, "tasksId", "parentId");
        let list = response.data;
        for (let index = 0; index < list.length; index++) {
          let task = {
            id: 0,
            text: "",
            start_date: "",
            end_date: "",
            progress: 0,
          };
          let link = {
            id: 0,
            source: 0,
            target: 0,
            type: "0",
          };
          task.id = list[index].tasksId;
          task.text = list[index].tasksName;
          task.start_date =
            list[index].planBeginTime == null
              ? this.getNowFormatDay()
              : list[index].planBeginTime;
          task.end_date =
            list[index].planEndTime == null
              ? this.getNowFormatDay()
              : list[index].planEndTime;

          task.progress = list[index].progress / 100;
          this.tasks.data.push(task);

          link.id = list[index].tasksId;
          link.target = list[index].tasksId;
          link.source = list[index].parentId;
          this.tasks.links.push(link);
        }
        this.$refs.gt.ganttInit();
        this.loading = false;
      });
    },

    //时间字符串相减
    DateMinus(date1, date2) {
      //date1:小日期   date2:大日期
      var sdate = new Date(date1);
      var now = new Date(date2);
      var days = now.getTime() - sdate.getTime();
      var day = parseInt(days / (1000 * 60 * 60 * 24));
      return day;
    },
    //获取当前时间
    getNowFormatDay(nowDate) {
      var char = "-";
      if (nowDate == null) {
        nowDate = new Date();
      }
      var day = nowDate.getDate();
      var month = nowDate.getMonth() + 1; //注意月份需要+1
      var year = nowDate.getFullYear();
      //补全0，并拼接
      return year + char + this.completeDate(month) + char + this.completeDate(day);
    },
    
   completeDate(value) {
        return value < 10 ? "0"+value:value;
    }
  },
};
</script>

<style>
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.container {
  width: 100%;
}
.left-container {
  overflow: hidden;
  position: relative;
  height: 100%;
}
</style>